// pages/doctorPages/my/mySet/mySet.js
const app = getApp() //获取app实列
const ajax = require("../../../../utils/ajax.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    accessToken:"",
    qrcode:""
  },
  clickQrcode: function () {
    let that = this
    wx.previewImage({
      current: that.data.qrcode, // 当前显示图片的http链接
      urls: [that.data.qrcode]
    })
  },
  updateClick:function(){
    wx.navigateTo({
      url: '/pages/serverRelName/serverRelName?type=update',
    })
  },
  getAccessToken:function(){
    this.setData({
      qrcode: "https://appapi.gongcing.com/doctorQcode.json?token="+wx.getStorageSync("token")
    })
  },
  getUserInfo: function () {
    let that = this
    ajax.request({
      url: app.url.userInfo
    }).then(res => {
      if (res.code == 200) {
        let str = JSON.stringify(res.data)
        wx.setStorageSync("userInfo", str)
        that.setData({
          userInfo: res.data
        })
      } else {
        return wx.showToast({
          title: res.msg,
          icon: "none"
        })
      }
    })
  },
  uploadImg: function () {
    let that = this;

    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        let info = JSON.parse(wx.getStorageSync("userInfo"))
        let token = wx.getStorageSync("token")
        wx.showLoading({
          title: '拼命上传中..',
        })
        wx.uploadFile({
          url: app.url.upload, //仅为示例，非真实的接口地址
          filePath: res.tempFilePaths[0],
          name: 'file',
          formData: {
            token: token
          },
          success(data) {
            info.avatar = JSON.parse(data.data).data.path
            console.log(info.avatar)
            ajax.request({
              url: app.url.userUpdateAvatar,
              data: info
            }).then(res => {
              if (res.code == 200) {
                that.getUserInfo()
                wx.hideLoading()
              }
            })
          }
        })
      }
    })
  },
  backLogin:function(){
    wx.removeStorageSync("token")
    wx.removeStorageSync("userInfo")
    wx.removeStorageSync("openid")
    wx.reLaunch({
      url: '/pages/login/login',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let user = wx.getStorageSync("userInfo");
    if (user) {
      this.setData({
        userInfo: JSON.parse(user)
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let token = wx.getStorageSync("token");
    this.setData({
      qrcode: app.url.doctorQcode+"?token="+token
    })
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getAccessToken();
    let user = wx.getStorageSync("userInfo");
    if (user) {
      this.setData({
        userInfo: JSON.parse(user)
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})